import Taro from '@tarojs/taro'
import {SET_CITY, AUTO_LOCATION} from '../constants/redux'
import { CityLocation } from '../utils/server.interface'
import { http } from '../utils/http'

let init_store: CityLocation = {
    area_name: '全国',
    region_id: 0,
}
const city = Taro.getStorageSync('city');
if(city){
    init_store = city
}

const location = (state = init_store, action) => {
    switch(action.type){
        case SET_CITY: 
            Taro.setStorageSync('city', action.city)
            http.areaId = action.city.region_id
            return {...state, ...action.city}
        case AUTO_LOCATION:
            Taro.setStorageSync('city', action.city)
            http.areaId = action.city.region_id
            return {...state, ...action.city}
        default: 
            return state
    }
}
export default location